package com.dengyuanke.spring.cloud.weather.vo;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.Reader;
import java.io.StringReader;

/**
 * Created with IntelliJ IDEA.
 * Author: dyk
 * Create time: 2018/9/6  13:27
 */
public class XmlBuilder {
    public static Object xmlStrToObject(Class<?> clazz,String xmlStr) throws Exception{
        Object xmlObject=null;
        Reader reader=null;
        JAXBContext context=JAXBContext.newInstance(clazz);
        //将Xml转成对象的核心接口
        Unmarshaller unmarshaller=context.createUnmarshaller();
        reader=new StringReader(xmlStr);
        xmlObject=unmarshaller.unmarshal(reader);
        if (null!=reader){
            reader.close();
        }
        return xmlObject;
    }
}
